-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add row_count()
to count specific values row-wise
#553
Conversation
I feel I'm always the one reluctant to add new functions here (sorry about that 😅) but this feels like a very narrow usecase, especially since it's possible to do it in a few lines (unless I'm missing sth): library(datawizard)
dat <- data.frame(
c1 = c("1", "2", NA, "3"),
c2 = c(NA, "2", NA, "3"),
c3 = c(NA, 4, NA, NA),
c4 = c(2, 3, 7, Inf)
)
data_modify(
dat,
count_2 = rowSums(dat == 2, na.rm = TRUE),
count_3 = rowSums(dat == 3, na.rm = TRUE)
)
#> c1 c2 c3 c4 count_2 count_3
#> 1 1 <NA> NA 2 1 0
#> 2 2 2 4 3 2 1
#> 3 <NA> <NA> NA 7 0 0
#> 4 3 3 NA Inf 0 2 I haven't looked at the other PRs on |
Yes, you're missing something :-) library(datawizard)
dat <- data.frame(
c1 = c("1", "2", NA, "3"),
c2 = c(NA, "2", NA, "3"),
c3 = c(NA, 4, NA, NA),
c4 = c(2, 3, 7, Inf)
)
data_modify(
dat,
count__2 = rowSums(dat == 2, na.rm = TRUE),
count_string_2 = rowSums(dat == "2", na.rm = TRUE)
)
#> c1 c2 c3 c4 count__2 count_string_2
#> 1 1 <NA> NA 2 1 1
#> 2 2 2 4 3 2 2
#> 3 <NA> <NA> NA 7 0 0
#> 4 3 3 NA Inf 0 0
row_count(dat, count = 2)
#> [1] 1 2 0 0
row_count(dat, count = "2")
#> [1] 1 2 0 0
row_count(dat, count = 2, exact = TRUE)
#> [1] 1 0 0 0
row_count(dat, count = "2", exact = TRUE)
#> [1] 0 2 0 0 |
Not that seldom use case if you work with single items that you want to turn into scales (something we do often)
Where would you add a feature for rowwise operations? And: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I should have read the changes before commenting ;)
A few comments
Great! What about the argument name |
I think |
row_count()
row_count()
to count specific values row-wise
Thanks! |
@etiennebacher not sure if you think
exact
is the best argument name, I'm open for any better suggestions.